package com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.ChapterDetail.Adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.ChapterDetail.ItemBean.Comment;

import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder>{
    private List<Comment> comments;
    public CommentAdapter(List<Comment> comments) {
        this.comments = comments;
    }
    @NonNull
    @Override
    public CommentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_chapter_detail_comment, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        //评论
        holder.tv_comment_content.setText(comments.get(position).getCommentContent());
        //姓名
        holder.tv_student_name.setText(comments.get(position).getName());
        //头像
        holder.iv_avatar.setImageResource(comments.get(position).getAvatar());
        //点赞数
        holder.tv_like_count.setText(comments.get(position).getLikeCount());
        holder.ll_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!holder.isLike){
                    holder.tv_like_count.setText(String.valueOf(Integer.parseInt(comments.get(position).getLikeCount())+1));
                    //设置点赞后的图片样式
//                    holder.iv_like.setImageResource(R.drawable.ic_like_fill);
                    //点赞后的字体样式
                    holder.tv_like_count.setTextColor(holder.itemView.getResources().getColor(R.color.colorPrimary));
                }else{
                    holder.tv_like_count.setText(String.valueOf(Integer.parseInt(comments.get(position).getLikeCount())));
                    holder.tv_like_count.setTextColor(holder.itemView.getResources().getColor(R.color.black));
                }
                holder.isLike=!holder.isLike;
            }
        });
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        boolean isLike;
        ImageView iv_avatar,iv_like;
        TextView tv_student_name,tv_comment_content,tv_like_count;
        LinearLayout ll_like;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_avatar = itemView.findViewById(R.id.iv_avatar);
            tv_comment_content = itemView.findViewById(R.id.tv_comment_content);
            tv_student_name = itemView.findViewById(R.id.tv_student_name);
            tv_like_count = itemView.findViewById(R.id.tv_like_count);
            ll_like = itemView.findViewById(R.id.ll_like);
        }
    }
}
